package jafou.client.ui.objects;

import jafou.client.remote.JafouService;
import jafou.client.remote.JafouServiceAsync;
import jafou.client.ui.ForumWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;

public class ContentPanel extends FlowPanel {
	
	ForumWidget ui;
	
	private final JafouServiceAsync handler = GWT.create(JafouService.class);
	
	String[][] topics = null;
	String[][] posts = null;
	
	private AsyncCallback<String[][]> topicsCallback = new AsyncCallback<String[][]>() {

		@Override
		public void onFailure(Throwable caught) {
			
		}

		@Override
		public void onSuccess(String[][] result) {
			topics = result;
			handleTopicsUpdateOnSuccess();
		}
		
	};
	
	private AsyncCallback<String[][]> postsCallback = new AsyncCallback<String[][]>() {

		@Override
		public void onFailure(Throwable caught) {
			
		}

		@Override
		public void onSuccess(String[][] result) {
			posts = result;
			handlePostsUpdateOnSuccess();
		}
		
	};
	
	private class TopicClickHandler implements ClickHandler {
		private String topicId;
		
		public TopicClickHandler(String topicId) {
			this.topicId = topicId;
		}
		@Override
		public void onClick(ClickEvent event) {
			ui.renderTopic(topicId);
		}
	}
	
	class TopicsTable extends Grid {
		TopicsTable(int rows) {
			super(rows, 1);
		}
		
		void setData(String[][] data) {
			for(int i=0; i< getRowCount(); i++) {
				FlowPanel main = new FlowPanel();
				SimplePanel titlePanel = new SimplePanel();
				titlePanel.addStyleName("title-panel");
				main.add(titlePanel);
				HTML html = new HTML(data[i][0]);
				if(data[i].length > 4) {
					html.addClickHandler(new TopicClickHandler(data[i][4]));
				}
				titlePanel.add(html);
				HorizontalPanel extraPanel = new HorizontalPanel();
				main.add(extraPanel);
				extraPanel.add(new Label(data[i][1]));
				extraPanel.add(new Label(data[i][2]));
				setWidget(i, 0, main);
			}
		}
	}
	
	public ContentPanel(ForumWidget ui) {
		this.ui = ui;
	}
	
	public void showForum(String forumId) {
		handler.getTopicsInForum(forumId, topicsCallback);
	}
	
	public void showTopic(String topicId) {
		handler.getPostsInTopic(topicId, postsCallback);
	}
	
	public void clearContents() {
		while(getWidgetCount() > 0) {
			remove(0);
		}
	}
	
	private void handleTopicsUpdateOnSuccess() {
		if(topics == null) return;
		clearContents();
		TopicsTable table = new TopicsTable(topics.length);
		table.setData(topics);
		add(table);
	}
	
	private void handlePostsUpdateOnSuccess() {
		if (posts == null) return;
		clearContents();
		TopicsTable table = new TopicsTable(posts.length);
		table.setData(posts);
		add(table);
	}

}
